import { useRef } from 'react'

type Callback<T> = (oldValue: T, newValue: T) => void

/**
 * 监听某个值，实现类似 vue 的 watch 功能
 * @date 2021-07-16
 * @param {any} state
 * @param {any} callback
 * @returns {any}
 */
function useWatch<T>(state: T, callback: Callback<T>) {
    const prevRef = useRef<any>()

    callback && callback(prevRef.current, state)
    prevRef.current = state
}


export default useWatch
